<?
header("Content-type:text/html;charset=utf-8");
ob_start();
include("common/header.html");
include("common/left.html");
$out_html=ob_get_contents();
ob_end_clean();
include("class/SimpleExcel.class.php");
include("class/OrderRecord.php");
include("class/ProOrder.php");
$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$s= new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));
$memberID=$_SESSION['sid'];
if($_GET['pzp']){
 echo $_SESSION['sdtype'];
}
$pOrder = $_REQUEST['atype']=='sale' ? saleProOrderObject($memberID,$s,$_SESSION['sdtype']) :buyProOrderObject($memberID,$s,$_SESSION['sdtype']);
// $pOrder=saleProOrderObject($memberID,$s,$_SESSION['sdtype']);
if($_REQUEST['count']){

	$currentPage = $_REQUEST['loadPage'];
	$pageSize = 1000;
	$r=$pOrder->_orderlist($currentPage,$pageSize);
	//print_r($r);
	//exit;
	foreach($r as $key=>$row){
		
		if($_SESSION['dtype']==2){// 直接供应商查看价格
			$r[$key]['mainOrder']->UUtprice = 
			$s->get_Dynamic_Price_Merge((string)$row['mainOrder']->UUpid,(string)$row['mainOrder']->UUbegintime,1);
		}
		
		if($r[$key]['mainOrder']->UUstatus!=1 && $_SESSION['sdtype']!=0){// 屏蔽未使用的手机号
			$r[$key]['mainOrder']->UUordertel=substr_replace((string)$r[$key]['mainOrder']->UUordertel,"****",3,4);
		}
		
		$fids[]=(string)$row['mainOrder']->UUbuyid;
		$r[$key]['totalPrice'] = ((string)$row['mainOrder']->UUtprice*(string)$row['mainOrder']->UUtnum);
		
	}
	$tmp = $r;
	$arr = array();
	foreach($r as $key => $row){
		// 处理
		$concat = (string)$row['mainOrder']->UUconcat_id;
		$orderid = (string)$row['mainOrder']->UUordernum;
		if($concat==0){
			$arr[] = $r[$key];// 主订单
			continue;
		}
		if($concat!=$orderid){
			$tmp[$key]= $r[$key];
			continue;
		}
		// 主订单并且含有从订单
		foreach($tmp as $k=>$data){
			if((string)$data['mainOrder']->UUconcat_id==$orderid && (string)$data['mainOrder']->UUordernum!=$orderid){
				$r[$key]['childOrder'][] = $data;
				unset($tmp[$k]);
			}
		}
		$arr[] = $r[$key];
	}
	$r = $arr;
	if($fids && ($_SESSION['dtype']!=2)){
		
		$sel="select id,dname from pft_member where id in (".implode(",",$fids).")";
		$fnames=array();
		$GLOBALS['le']->query($sel);
		while($row=$GLOBALS['le']->fetch_assoc()){
			$fnames[$row['id']]=$row['dname'];
		}
	}
	$filename = $_REQUEST['filename'];
	$filename.='-'.$_REQUEST['loadPage'];
	include("loadExcel_simple.php");
	exit;
}

$r=$pOrder->_orderlist(1,1);
$count=$pOrder->orderlistTotal();
$loadCount = ceil(($count/1000)) + 1;
echo $out_html;

$filename = date('YmdHis');
$filename.= ($_REQUEST['atype']=='buy') ? '买到产品订单列表':'卖出产品订单列表';

?>
<title>数据导出</title>
<link rel="stylesheet" type="text/css" href="css/default.css"/>
<link rel="stylesheet" href="css/buyProduct.css" />
<style>
p{margin:10px 0;font-size:1em;}
td{padding:10px;border-bottom:1px solid #EEE;}
</style>
<div class="rigArea">
	<div class="topTit"><span class="txt"><strong>数据导出</strong></span></div>

	<p>匹配到数据：<?=$count?> 条数据，请分批次导出，每次 1000 条。</p>
	<form target="main" class="load_form">
	<input type="hidden" name="count" value="<?=$count?>" />
	<input type="hidden" class="loadPage" name="loadPage" value="1"/>
	<input type="hidden" name="filename" value="<?=$filename?>"/>
	<?
		
		foreach($_REQUEST as $key=>$value){
			if($key=='status'){
				$tmp = explode('|',$value[0]);
				if(count($tmp)){
					foreach($tmp as $k=>$v){
						echo '<input type="hidden" name="'.$key.'[]" value="'.$v.'"/>';
					}
				}
			}else{
				echo '<input type="hidden" name="'.$key.'" value="'.$value.'"/>';
			}
		}
	?>

	<table class="proTable ">
		<thead id="proTableHead">
			<th class="col_1">批次号</th>
			<th class="col_2" style="text-align:left;padding-left:20px;">文件名</th>
			<th class="col_3">操作</th>
		</thead>
		<tbody>
			<?for($i=1;$i<$loadCount;$i++):?>
			<tr>
				<td><?=$i?></td>
				<td style="text-align:left;padding-left:20px;"><?=$filename.'-'.$i?>.xls</td>
				<td><a href="javascript:void(0);" data="<?=$i?>" onclick="changeLoad(this);">导出</a></td>
			</tr>
			<?endfor?>
		</tbody>
	</table>
</div>
<iframe name="main" style="display:none;"></iframe>
<script>
function changeLoad(object){
	$(".loadPage").val(Number($(object).attr("data")));
	$(".load_form").submit();
}
</script>
<?include("common/footer.html");?>